package com.wolfsea.launcherdemo
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.get

/**
 *@desc  TopContentLayoutTop
 *@author liuliheng
 *@time 2023/1/30  16:30
 **/
class TopContentLayoutTop @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : ViewGroup(context, attributeSet), View.OnClickListener {

    init {
        inflate(context, R.layout.top_content_top_layout, this)

        findViewById<ConstraintLayout>(R.id.layout01).setOnClickListener(this)
        findViewById<ConstraintLayout>(R.id.layout02).setOnClickListener(this)
        findViewById<ConstraintLayout>(R.id.layout03).setOnClickListener(this)
        findViewById<ConstraintLayout>(R.id.layout04).setOnClickListener(this)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var totalWidth = 0
        var totalHeight = 0
        for (i in 0.until(childCount)) {
            val child = get(i)
            measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0)
            val childWidth = child.measuredWidth
            val childHeight = child.measuredHeight
            val childLayoutParams = child.layoutParams as MarginLayoutParams
            totalWidth = Math.max(
                0,
                childWidth + childLayoutParams.leftMargin + childLayoutParams.rightMargin
            )
            totalHeight += childHeight + childLayoutParams.topMargin + childLayoutParams.bottomMargin
        }
        setMeasuredDimension(
            MeasureSpec.makeMeasureSpec(
                totalWidth,
                MeasureSpec.EXACTLY
            ) - paddingStart - paddingEnd,
            MeasureSpec.makeMeasureSpec(
                totalHeight,
                MeasureSpec.EXACTLY
            ) - paddingTop - paddingBottom
        )
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        for (i in 0.until(childCount)) {
            val child = get(i)
            val childMeasuredWith = child.measuredWidth
            val childMeasuredHeight = child.measuredHeight
            val layoutParams = child.layoutParams as MarginLayoutParams
            child.layout(
                layoutParams.leftMargin,
                layoutParams.topMargin,
                layoutParams.leftMargin + childMeasuredWith,
                layoutParams.topMargin + childMeasuredHeight
            )
        }
    }

    override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
        return MarginLayoutParams(context, attrs)
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.layout01 -> {
                context.showToast("layout01")
            }
            R.id.layout02 -> {
                context.showToast("layout02")
            }
            R.id.layout03 -> {
                context.showToast("layout03")
            }
            R.id.layout04 -> {
                context.showToast("layout04")
            }
        }
    }

}